import { defineStore } from 'pinia'
import { UseLoginApi, getUserInfoApi } from '@/api/login'
import { Local } from '@/utils/storage'

/**
 * 模板列表
 * @methods setTemplatesList 设置模板数据
 */
export const userInfos = defineStore('users', {
  state: (): UserProps => ({
    token: Local.get('token') || '',
    isLogin: false,
    userInfos: null
  }),
  actions: {
    async login(payload: any) {
      const [err, r] = await UseLoginApi.signIn<string>(payload, { opName: 'login' })
      if (r && r.errno == 0) {
        const token = r.data
        this.token = token
        Local.set('token', token)

        //Session.set('token', token)
        //axios.defaults.headers.common.Authorization = `Bearer ${token}`
      }
    },
    logout() {
      this.token = ''
      this.isLogin = false
      Local.clear()
      //delete axios.defaults.headers.common.Authorization
    },
    async setUserInfos() {
      const [err, r] = await getUserInfoApi<UserDataProps>()
      if (r && r.errno === 0) {
        this.userInfos = r.data
        Local.set('userInfo', this.userInfos)
        this.isLogin = true
      }
    }
  }
})
